local lusionsql = require "tmysql"
if not lusionsql then return else


	local Rp_DB

	local function LusionRp_SQLConnect()
		tmysql.initialize(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT)
	end
	hook.Add("PlayerInitialSpawn", "lolfasdfsd", function( pl )
		if #player.GetAll() > 0 then
			LusionRp_SQLConnect()
		end
	end)

	function LusionRp_SQLSpawn( Player )
		if ValidEntity( Player ) and Player:IsPlayer() then
			local query1 = tmysql.query("SELECT * FROM roleplaydata WHERE uniqueid='" .. Player:NSteamID() .. "'")
			if ( query1 ) then
				print("Loaded info successfully: " .. Player:RealName() .. " (" .. Player:SteamID() .. ")")
			else
				tmysql.query("INSERT INTO roleplaydata (uniqueid, lusionid, steamid, rpname, money, isarrested, hasphysgun, cansuicide) VALUES ('" .. Player:NSteamID() .. "', '" .. Player:GetNWInt("lusion_id") .. "', '" .. Player:SteamID() .. "', '" .. Player:GetNWString("Lusion_Nickname") .. "', '" .. GAMEMODE.config["Starting Wallet"] .. "', '" .. tostring(false) .. "', '" .. tostring(false) .. "', '" .. tostring(false) .. "')")
				print("Created info for: " .. Player:RealName() .. " (" .. Player:SteamID() .. ")")
			end
		end
	end
	
		-- Updating
	function LusionRp_UpdatePlayer( Player )
		if ValidEntity( Player ) and Player:IsPlayer() then
			tmysql.query("INSERT INTO roleplaydata (uniqueid, lusionid, steamid, rpname, money, isarrested, hasphysgun, cansuicide) VALUES ('" .. Player:NSteamID() .. "', '" .. Player:GetNWInt("lusion_id") .. "', '" .. Player:SteamID() .. "', '" .. Player:GetNWString("Lusion_Nickname") .. "', '" .. Player:GetNWInt("currency") .. "', '" .. tostring(false) .. "', '" .. tostring(false) .. "', '" .. tostring(false) .. "')")
		--	tmysql.query("UPDATE roleplaydata SET money='" .. Player:GetNWInt("currency") .. "' WHERE uniqueID='" .. Player:NSteamID() .. "'")
			print("Attempted to update money for: " .. Player:RealName() .. " (" .. Player:SteamID() .. ")")
		end
	end
	function LusionRp_UpdateAll( Player )
		if ValidEntity( Player ) and Player:IsPlayer() then
			tmysql.query("UPDATE roleplaydata SET money='" .. Player:GetNWInt("currency") .. "', rpname='" .. Player:GetNWString("Lusion_Nickname") .. "', hasphysgun='" .. tostring(Player:IsOwner()) .. "' WHERE uniqueid='" .. Player:NSteamID() .. "'")
			print("Attempted to update table for: " .. Player:RealName() .. " (" .. Player:SteamID() .. ")")
		end
	end
	-- Retrieving
	
	function LusionRp_GetMoney( Player )
		if ValidEntity( Player ) and Player:IsPlayer() then
			local query1 = tmysql.query("SELECT money FROM roleplaydata WHERE uniqueid='" .. Player:NSteamID() .. "'")
			return query1
		end
	end
	
	
end

print("Loading serverside sql.lua!")